{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如何创建工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={'source': 'Data/温度.xlsx'}, page_content='\\n\\n\\n月份\\n温度\\n\\n\\n1\\n18\\n\\n\\n2\\n19\\n\\n\\n3\\n20\\n\\n\\n4\\n20\\n\\n\\n5\\n22\\n\\n\\n6\\n23\\n\\n\\n7\\n27\\n\\n\\n8\\n33\\n\\n\\n9\\n33\\n\\n\\n10\\n22\\n\\n\\n11\\n20\\n\\n\\n12\\n3\\n\\n\\n')]\n"
     ]
    }
   ],
   "source": [
    "from langchain_community.document_loaders.csv_loader import CSVLoader\n",
    "from langchain_community.document_loaders import UnstructuredExcelLoader\n",
    "from crewai.tools import BaseTool\n",
    "from crewai import Agent, Task, Crew, LLM, Process\n",
    "\n",
    "my_llm = LLM(\n",
    "    api_key = \"NA\",\n",
    "    model = \"ollama/qwen2.5:14b\",\n",
    "    base_url = \"http://192.168.20.43:11434\"\n",
    ")\n",
    "\n",
    "#loader = CSVLoader(file_path=\"/Data/学生信息.xlsx\")\n",
    "# loader = UnstructuredExcelLoader(file_path=\"/Data/学生信息.xlsx\")\n",
    "file_path=\"Data/温度.xlsx\"\n",
    "\n",
    "class ExcelLoaderTool(BaseTool):\n",
    "    name: str = \"ExcelLoaderTool\"\n",
    "    description: str = \"A tool to load data from a predefined Excel file using UnstructuredExcelLoader.\"\n",
    "\n",
    "    def _run(self) -> str:\n",
    "        \"\"\"Load data from the predefined Excel file.\"\"\"\n",
    "        try:\n",
    "            # 固定的文件路径\n",
    "            loader = UnstructuredExcelLoader(file_path=file_path)\n",
    "            data = loader.load()\n",
    "            # 将数据转换为字符串格式返回（这里可以根据需要调整返回格式）\n",
    "            return str(data)\n",
    "        except Exception as e:\n",
    "            return f\"Error loading Excel file: {str(e)}\"\n",
    "\n",
    "# 创建工具实例\n",
    "excel_loader_tool = ExcelLoaderTool()\n",
    "print(excel_loader_tool._run())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
